import 'package:flutter/material.dart';
import 'package:shenling_logistics/utils/token.dart';

class SystemSettingPage extends StatefulWidget {
  const SystemSettingPage({super.key});

  @override
  State<SystemSettingPage> createState() => _SystemSettingPageState();
}

class _SystemSettingPageState extends State<SystemSettingPage> {
  // 定义设置页面功能栏数据
  List navList = [
    {'title': '换绑手机'},
    {'title': '修改密码'},
    {'title': '消息通知设置'},
    {'title': '清理缓存'},
  ];

  // 定义退出登录方法
  logout() {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          backgroundColor: Colors.white,
          content: Text('确定要退出登录吗?'),
          actions: [
            TextButton(
              onPressed: () {
                // 关闭弹层
                Navigator.pop(context);
              },
              child: Text('取消', style: TextStyle(color: Colors.grey)),
            ),
            TextButton(
              onPressed: () {
                // 1.将token清除
                TokenManager().removeToken();
                // 2.关闭弹层
                Navigator.pop(context);
                // 3.跳转到登录页
                Navigator.pushNamed(context, '/login');
              },
              child: Text('退出', style: TextStyle(color: Colors.red)),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('系统设置'), backgroundColor: Colors.white),
      body: Container(
        padding: EdgeInsets.only(left: 15, right: 15, top: 20),
        color: Color.fromARGB(255, 244, 244, 244),
        child: Column(
          children: [
            // 设置页功能栏
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
              ),
              child: Column(
                children: navList.map((item) {
                  return Container(
                    padding: EdgeInsets.all(15),
                    child: Row(
                      children: [
                        // 左侧标题
                        Text(item['title']),
                        Spacer(),
                        // 右侧图标
                        Icon(Icons.arrow_forward_ios, color: Colors.grey),
                      ],
                    ),
                  );
                }).toList(),
              ),
            ),
            SizedBox(height: 40),
            // 退出登录按钮
            SizedBox(
              width: MediaQuery.of(context).size.width,
              height: 60,
              child: ElevatedButton(
                onPressed: () {
                  logout();
                },
                style: ButtonStyle(
                  backgroundColor: WidgetStateProperty.all(Colors.white),
                ),
                child: Text('退出'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
